package everyday.m20230309;

/**
 * leetcode 2379题
 */
public class Q20230309 {

    public static void main(String[] args) {
        Q20230309 q20230309 = new Q20230309();
        int wbbwwbbwbw = q20230309.minimumRecolors("WWBBBWBBBBBWWBWWWB", 16);
        System.out.println(wbbwwbbwbw);
    }

    public int minimumRecolors(String blocks, int k) {
        // 滑动窗口，固定一个k大小的判断里面有几个b
        int res = 0;
        for (int i = 0; i < k; i++) {
            if (blocks.charAt(i) == 'W') {
                res ++;
            }
        }
        // 继续滑动
        int prev = res;
        for (int i = 1; i <= blocks.length() - k; i++) {

            if (blocks.charAt(i - 1) == 'W') {
                prev --;
            }
            if (blocks.charAt(i + k - 1) == 'W') {
                prev ++;
            }
            res = Integer.min(res, prev);
        }
        return res;
    }
}
